<template>
  <el-dialog v-el-drag-dialog title="分配菜单" :visible.sync="visible" :append-to-body="appendToBody" :before-close="onClose">
    <el-form ref="roleForm" :model="form" label-width="120px">
      <el-form-item hidden>
        <el-input v-model="form.roleId" hidden />
      </el-form-item>

      <el-card>
        <el-form-item label="角色名称">
          <el-input v-model="form.roleName" disabled />
        </el-form-item>
        <el-form-item label="权限字符">
          <el-input v-model="form.roleKey" disabled />
        </el-form-item>
        <el-form-item label="菜单列表">
          <el-card>
            <el-tree
              ref="authMenuTree"
              node-key="id"
              :data="treeDate"
              :props="treeProps"
              class="filter-tree"
              show-checkbox
              check-strictly
              :default-expand-all="false"
              :default-expanded-keys="defaultExpandedKeys"
            />
          </el-card>
        </el-form-item>
      </el-card>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button v-if="hasLoaded" type="primary" icon="el-icon-check" @click="onSubmit">确 定</el-button>
      <el-button icon="el-icon-close" @click="onClose">关 闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { roleLoad, saveRoleMenu } from '@/api/system/role'
import { menuTreeData } from '@/api/system/menu'

export default {
  name: 'AuthMenuDialog',
  data() {
    return {
      visible: false,
      hasLoaded: false,
      appendToBody: false,
      form: {
        roleId: undefined,
        roleName: undefined,
        roleKey: undefined,
        menuIds: undefined
      },
      treeDate: [],
      treeProps: {
        children: 'children',
        label: 'name'
      },
      defaultExpandedKeys: ['0']
    }
  },
  methods: {
    init(roleId) {
      this.hasLoaded = false
      this.form.roleId = roleId || null
      this.visible = true
      this.$nextTick(() => {
        this.loadForm(roleId)
      })
    },
    loadForm(roleId) {
      const that = this
      this.$http.all([roleLoad({ 'id': roleId }), menuTreeData({ from: 'vue' })])
        .then(this.$http.spread((resForm, resTree) => {
          that.form = that.$api.objectUpdate(that.form, resForm.param)
          that.treeDate = resTree
          if (that.form.menuIds) {
            that.$nextTick(() => {
              that.defaultExpandedKeys = that.form.menuIds.length > 0 ? (that.form.menuIds).split(',') : [that.form.menuIds]
              that.$refs.authMenuTree.setCheckedKeys(that.defaultExpandedKeys)
              that.$refs.authMenuTree.setCurrentKey(that.defaultExpandedKeys)
            })
          }
          // 判断当前用户权限
          this.hasLoaded = this.$perm.hasPermissionOr(['system:role:grant'])
      })).catch(() => {})
    },
    onSubmit() {
      const checked = this.$refs.authMenuTree.getCheckedNodes()
      const menuIds = checked.map(node => node.id).join(',')
      saveRoleMenu({
        roleId: this.form.roleId,
        menuIds: menuIds.length === 1 ? undefined : menuIds
      }).then(response => {
        this.$modal.msgSuccess(response.msg)
        this.$emit('confirmForm')
        this.onClose()
      }).catch(() => {})
    },
    onClose() {
      this.visible = false
      this.$refs.roleForm.resetFields()
      this.treeDate = []
    }
  }
}
</script>
